import VueClipboard from "clipboard";

export class Utils {
    //参数处理
    static tansParams(params) {
        let result = ''
        for (const propName of Object.keys(params)) {
            const value = params[propName]
            const part = encodeURIComponent(propName) + '='
            if (value !== null && value !== '' && typeof value !== 'undefined') {
                if (typeof value === 'object') {
                    for (const key of Object.keys(value)) {
                        if (
                            value[key] !== null &&
                            value[key] !== '' &&
                            typeof value[key] !== 'undefined'
                        ) {
                            const params = propName + '[' + key + ']'
                            const subPart = encodeURIComponent(params) + '='
                            result += subPart + encodeURIComponent(value[key]) + '&'
                        }
                    }
                } else {
                    result += part + encodeURIComponent(value) + '&'
                }
            }
        }
        return result
    }

    // 返回项目路径
    static getNormalPath(p) {
        if (p.length === 0 || !p || p === 'undefined') {
            return p
        }
        let res = p.replace('//', '/')
        if (res[res.length - 1] === '/') {
            return res.slice(0, res.length - 1)
        }
        return res
    }

    // 数据结构转换
    static convertTreeData(data) {
        for (let i = 0; i < data.length; i++) {
            if (!data[i].children || data[i].children.length === 0) {
                data[i].children = null
            } else {
                Utils.convertTreeData(data[i].children)
            }
        }
        return data
    }

    //筛选树形数据
    static filterTreeData(data, value) {
        let res = null
        for (let i = 0; i < data.length; i++) {
            if (data[i].id === value) {
                res = data[i]
                break
            } else if (data[i].children && data[i].children.length > 0) {
                const child = Utils.filterTreeData(data[i].children, value)
                if (child) {
                    res = child
                    break
                }
            }
        }
        return res
    }

    //字符串脱敏
    static maskedStr(str, start, end, maskChar = '*') {
        if (str === null || str === undefined) {
            return ''
        }
        if (start < 0) {
            start = 0
        }
        if (end > str.length) {
            end = str.length
        }
        if (start > end) {
            const temp = start
            start = end
            end = temp
        }
        const leftStr = str.substring(0, start)
        return leftStr + maskChar.repeat(str.length - end - start) + str.substring(str.length - end)
    }

    //截取小数位数
    static truncateDecimals(value, decimals) {
        const factor = Math.pow(10, decimals)
        return Math.floor(value * factor) / factor
    }

    //以万计算
    static calculatedThousands(value) {
        if (!value) {
            return 0
        } else if (value < 10000) {
            return value
        } else {
            let num = value / 10000
            return parseFloat(num.toFixed(2)) + 'W'
        }
    }

    //字符串字符替换
    static strReplace(value, regex, newRegex) {
        if (value === null || value === undefined) {
            return value;
        }
        return value.replace(regex, newRegex);
    }

    //复制
    static handleCopy(proxy, val) {
        if (val) {
            VueClipboard.copy(val);
            proxy.$message.success("复制成功");
        } else {
            proxy.$message.error("复制失败");
        }
    }

    /*
     * @description 生成随机数
     * @param {number} min 最小值
     * @param {number} max 最大值
     * @returns {number}
     */
    static randomNum(min, max) {
        return Math.floor(Math.random() * (min - max) + max)
    }

    //0填充字符串
    static zeroStr(length) {
        let str = '';
        for (let i = 0; i < length; i++) {
            str += '0';
        }
        return str;
    }
}



